import router from "./routers";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

import { getToken } from "@/utils/auth";

NProgress.configure({ showSpinner: false });

const whiteList = ["/login", "/register", "/forgetPassword", "enterTeam"];

router.beforeEach((to, from, next) => {
  // 进度条
  NProgress.start();

  if (getToken()) {
    // 已登录且要跳转的页面是登录页
    next();
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next();
    } else {
      next(`/login`); // 否则全部重定向到登录页
    }
  }
});
